"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，无任何业务压力, switchover -n 指定节点主备切换
Create At   : 2024-04-07
Owner       : songshixuan
Description :
    1、查看集群状态 cm_ctl query -Cidvp
    2、备1节点执行switchover切换为主节点 cm_ctl switchover -n 2 -D xxx/dn1
    3、再次查看切换后集群状态 cm_ctl query -Cidvp
    4、恢复集群原始状态 cm_ctl switchover -a
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、switchover执行成功
    3、集群状态正常,备1节点切换为主节点，原主节点降为备节点
    4、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-04-07 创建
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:备1节点执行switchover切换为主节点; expect:执行成功-----'
        self.log.info(text)
        switch = self.s1_comsh.exec_cm_ctl(mode='switchover',
                                           param='-n 2',
                                           path=f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switch)
        self.assertIn(self.constant.cm_switchover_success_msg, switch,
                      '执行失败' + text)

        text = '-----step3:再次查看切换后集群状态 expect:集群状态正常,备1切换为主节点，原主节点降为备节点-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        res1 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        res2 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.log.info(res1)
        self.log.info(res2)
        self.assertIn('S Primary Normal', res1, '执行失败' + text)
        self.assertIn('P Standby Normal', res2, '执行失败' + text)

    def tearDown(self):
        text = '-----step4:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='stop', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)
        
        recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                      '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
